﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.NCS
{
    public partial class FormQuanLyKhoaHoc : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        bool reload = false;
        public FormQuanLyKhoaHoc()
        {
            InitializeComponent();
            gridDataKhoaHoc.AddRowIndexing();
            InitGridKhoaHoc();
            LoadKhoaHoc();
        }

        void InitGridKhoaHoc()
        {
            gridDataKhoaHoc.AutoGenerateColumns = false;
            colTenKhoaHoc.DataPropertyName = "TenKhoaHoc";
            colNamTuyenSinh.DataPropertyName = "NamTuyenSinh";
        }



        void LoadKhoaHoc()
        {
            allowUpdate = false;
            var ls = BizKhoaHocNCS.GetList();
            bdKhoaHoc.DataSource = ls;
            gridDataKhoaHoc.DataSource = bdKhoaHoc;
            if (ls != null && ls.Count > 0)
            {
                btnSuaKhoaHoc.Enabled = btnXoaKhoaHoc.Enabled = true;
            }
            else
            {
                btnSuaKhoaHoc.Enabled = btnXoaKhoaHoc.Enabled = false;
            }
        }

        private void bdKhoaHoc_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                var current = bdKhoaHoc.Current as KhoaHocNCS;
            }
        }

        private void btnThemKhoaHoc_Click(object sender, EventArgs e)
        {
            var fdg = new FormQuanLyKhoaHoc_ChiTiet();
            fdg.OnChanged += (o, t) =>
            {
                reload = true;
                LoadKhoaHoc();
            };
            fdg.ShowDialog();
        }

        private void btnSuaKhoaHoc_Click(object sender, EventArgs e)
        {
            var current = bdKhoaHoc.Current as KhoaHocNCS;
            if (current != null)
            {
                var fdg = new FormQuanLyKhoaHoc_ChiTiet(current);
                if (fdg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    reload = true;
                    LoadKhoaHoc();
                }
            }
        }

        private void btnXoaKhoaHoc_Click(object sender, EventArgs e)
        {
            var current = bdKhoaHoc.Current as KhoaHocNCS;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizKhoaHocNCS.Delete(current);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        reload = true;
                        bdKhoaHoc.RemoveCurrent();
                    }
                    else
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.KHOAHOCNCS));
                }
            }
        }

        private void FormQuanLyKhoaHoc_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (reload)
                DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
